package com.bi.bitest.bao.fragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.android.volley.toolbox.ImageLoader;
import com.bi.bitest.bao.AlbumPlayActivity;
import com.bi.bitest.bao.Constants;
import com.bi.bitest.bao.R;
import com.bi.bitest.bao.adapters.Item_listDetailAdapter;
import com.bi.bitest.bao.entity.AlbumDetail.Album_AlbumDetail;
import com.bi.bitest.bao.entity.AlbumDetail.Tracks;
import com.bi.bitest.bao.tasks.AlbumDetailTask;
import com.bi.bitest.bao.tasks.TaskCallback;
import com.bi.bitest.bao.tasks.TaskResult;
import com.bi.bitest.bao.utils.VolleyUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class AlbumDetailFragment extends Fragment implements TaskCallback, AdapterView.OnItemClickListener {
    private long albumId;

   private TextView albumDetail;
    private TextView nickName;
    private TextView intro;
    private ImageView large;
    private ImageView small;
    private ListView listView;
    private ArrayList<Tracks>datas;
    private Item_listDetailAdapter adapter;


        public AlbumDetailFragment() {
        }

        public static AlbumDetailFragment newInstance(long str) {
           Bundle args=new Bundle();
            args.putLong("id", str);
            AlbumDetailFragment f=new AlbumDetailFragment();
            f.setArguments(args);
            return f;
        }

    @Override
    public void onCreate(Bundle savedInstanceState) {

         albumId=getArguments().getLong("id");
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_album_detail, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        initView();//一定要在oncreate View之后find**************************************
        AlbumDetailTask task=new AlbumDetailTask(this,albumId);
        task.execute();
        super.onActivityCreated(savedInstanceState);
    }
    private void initView() {
        albumDetail= (TextView) getActivity().findViewById(R.id.tv_title_album_detail);
        nickName=(TextView)getActivity().findViewById(R.id.tv_nickname_album_detail);
        intro= (TextView) getActivity().findViewById(R.id.tv_intro_album_detail);
        large= (ImageView) getActivity().findViewById(R.id.iv_large_album_detail);
        small= (ImageView) getActivity().findViewById(R.id.iv_small_album_detail);
        listView= (ListView) getActivity().findViewById(R.id.listView_album_detail);

        datas=new ArrayList<Tracks>();
        adapter=new Item_listDetailAdapter(getActivity(),datas);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(this);

    }



    @Override
    public void onTaskFinished(TaskResult taskResult) {
        if(taskResult !=  null) {
            if (taskResult.action == Constants.TASK_ALBUMDETAIL) {
                if (taskResult.retCode == Constants.TASK_RESULT_OK) {
                    Object data = taskResult.data;
                    if (data != null) {
                        if (data instanceof Album_AlbumDetail) {
                            Album_AlbumDetail album = (Album_AlbumDetail) data;
                            albumDetail.setText(album.getTitle());
                            nickName.setText(album.getNickname());
                            intro.setText(album.getIntro());
                            VolleyUtils.getLoader(getActivity()).get(album.getCoverLarge(), ImageLoader.getImageListener(
                                    large, R.mipmap.ic_launcher, android.R.drawable.ic_menu_delete));
                            VolleyUtils.getLoader(getActivity()).get(album.getAvatarPath(), ImageLoader.getImageListener(
                                    small, R.mipmap.ic_launcher, android.R.drawable.ic_menu_delete));
                            //listView
                            datas.addAll(album.getList());

                            adapter.notifyDataSetChanged();
                            setListViewHeightBasedOnChildren(listView);
                        }
                    }
                }

            } else {
                // TODO 提示错误信息
            }
        }
    }






    /**
     * 重新计算listView高度
     * @param listView
     */
    public  void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent=new Intent(getActivity(), AlbumPlayActivity.class);
        intent.putExtra("id",String.valueOf(id));
        intent.putExtra("position",position);
        intent.putParcelableArrayListExtra("tracks",datas);
        startActivity(intent);
    }
}
